class UserController {
	def userService
	
    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST", reminderPassword: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [userInstanceList: User.list(params), userInstanceTotal: User.count()]
    }

    def create = {
        def userInstance = new User()
        userInstance.properties = params
        return [userInstance: userInstance]
    }

    def save = {
		def result = userService.add(request.getFile('file'), params)
		if(!result.instance.hasErrors()){
			flash.message = "user.created"
            flash.args = [result.instance.id]
            flash.defaultMessage = "User ${result.instance.id} created"
            redirect(action: "show", id: result.instance.id)
    	}
		else { render(view: "create", model: [userInstance: result.instance]) }
    	return
    }

    def show = {
        def userInstance = User.get(params.id)
        if (!userInstance) {
            flash.message = "user.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "User not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [userInstance: userInstance]
        }
    }

    def edit = {
        def userInstance = User.get(params.id)
        if (!userInstance) {
            flash.message = "user.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "User not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [userInstance: userInstance]
        }
    }

    def enable = {
    	params.disabled = false
    	def result = userService.update(null, params)
    	if(result != null) {
        	if(result.hasErrors()){
        		render(view: "edit", model: [userInstance: result])
        	}else{
        		flash.message = "user.updated"
                flash.args = [params.id]
                flash.defaultMessage = "User ${params.id} updated"
                redirect(action: "show", id: result.id)
        	}
        }else{
            flash.message = "user.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "User not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }
    
    def disable = {
    	params.disabled = true
    	def result = userService.update(null, params)
    	if(result != null) {
        	if(result.hasErrors()){
        		render(view: "edit", model: [userInstance: result])
        	}else{
        		flash.message = "user.updated"
                flash.args = [params.id]
                flash.defaultMessage = "User ${params.id} updated"
                redirect(action: "show", id: result.id)
        	}
        }else{
            flash.message = "user.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "User not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }
	
    def update = {
        def result = userService.update(request.getFile('file'), params)
        if(result != null) {
        	if(result.hasErrors()){
        		render(view: "edit", model: [userInstance: result])
        	}else{
        		flash.message = "user.updated"
                flash.args = [params.id]
                flash.defaultMessage = "User ${params.id} updated"
                redirect(action: "show", id: result.id)
        	}
        }else{
            flash.message = "user.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "User not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
		def result = userService.delete(params)
		if (result != null) {
			if (!result.instance.hasErrors()) {
				session.invalidate()
				flash.message = "user.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "User ${params.id} deleted"
                redirect(action: "list")
                
			} else {
				flash.message = "user.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "User ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
			}
		} else {
			flash.message = "user.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "User not found with id ${params.id}"
            redirect(action: "list")	
		}
    }
}
